package com.wolves.career.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wolves.career.entity.IndustryInformationClassification;
import com.wolves.career.entity.IndustryInformationInformation;
import com.wolves.career.service.IndustryInformationClassificationService;
import com.wolves.career.service.IndustryInformationInformationService;
import com.wolves.career.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 * 资讯模块（普通用户，维护人员）
 *
 * @author 麦翔盛
 * @since 2021-10-20
 */
@RestController
@RequestMapping("/info")
@CrossOrigin
public class IndustryInformationInformationController {
    @Autowired
    private IndustryInformationClassificationService classificationService;
    @Autowired
    private IndustryInformationInformationService informationService;

    /**
     * 分页查询咨询
     *
     * @param pageNo      请求页码
     * @param limit       限制数量
     * @param information 封装请求咨询
     * @return
     */
    @PostMapping("/pageInformation/{pageNo}/{limit}")
    public R pageInformation(@PathVariable long pageNo,
                             @PathVariable long limit,
                             @RequestBody(required = false) IndustryInformationInformation information) {
        Page<IndustryInformationInformation> page = new Page<>(pageNo, limit);
        QueryWrapper<IndustryInformationInformation> queryWrapper = informationService.getInfo(information);
        queryWrapper.orderByAsc("update_time");
        IPage<IndustryInformationInformation> infoPage = informationService.page(page, queryWrapper);
        List<IndustryInformationInformation> records = infoPage.getRecords();
        long total = infoPage.getTotal();
        return R.ok().data("records", records).data("total", total);
    }

    /**
     * 获取得热度前几的咨询
     * @param limit 查询数量
     * @return
     */
    @GetMapping("/getHotInfo/{limit}")
    public R getHotInformation(@PathVariable long limit) {
        //分页获取前五的数据
        Page<IndustryInformationInformation> page = new Page<>(1, limit);
        QueryWrapper<IndustryInformationInformation> queryWrapper = new QueryWrapper<>();
        //降序排序
        queryWrapper.orderByDesc("views");
        IPage<IndustryInformationInformation> infoPage = informationService.page(page, queryWrapper);
        List<IndustryInformationInformation> records = infoPage.getRecords();
        return R.ok().data("list", records);
    }


    /**
     * 添加咨询
     *
     * @param information 资讯类
     * @return
     */
    @PostMapping("/addInformation")
    public R addInformation(@RequestBody(required = true)
                                    IndustryInformationInformation information) {
        boolean save = informationService.save(information);
        if (!save) {
            return R.error().message("添加咨询失败").data("success", false);
        }
        return R.ok().data("success", true);
    }

    /**
     * 根据id删除咨询
     *
     * @param informationId 咨询id
     * @return
     */
    @DeleteMapping("/deleteInformation/{informationId}")
    public R deleteInformation(@PathVariable String informationId) {
        boolean remove = informationService.removeById(informationId);
        if (!remove) {
            return R.error().message("删除失败").data("success", true);
        }
        return R.ok().data("success", true);
    }

    /**
     * 更新咨询
     *
     * @param information 信息类
     * @return
     */
    @PutMapping("/updateInformation")
    public R updateInformation(@RequestBody(required = true)
                                       IndustryInformationInformation information) {
        boolean update = informationService.updateById(information);
        if (!update) {
            return R.error().message("咨询更新失败").data("success", false);
        }
        return R.ok().data("success", true);
    }

    /**
     * 添加分类
     *
     * @param classification 分类类
     * @return
     */
    @PostMapping("/addClassification")
    public R addClassification(@RequestBody(required = false)
                                       IndustryInformationClassification classification) {
        boolean save = classificationService.save(classification);
        if (!save) {
            return R.ok().message("添加失败").data("success", false);
        }
        return R.ok().data("success", true);
    }

    /**
     * 根据id删除分类
     *
     * @param classificationId
     * @return
     */
    @DeleteMapping("/deleteClassification/{classificationId}")
    public R deleteClassification(@PathVariable String classificationId) {
        boolean remove = classificationService.removeById(classificationId);
        if (!remove) {
            return R.error().message("删除失败").data("success", false);
        }
        return R.ok().data("success", true);
    }

    /**
     * 更新咨询
     *
     * @param classification 资讯类
     * @return
     */
    @PutMapping("/updateClassification")
    public R updateClassification(@RequestBody(required = true)
                                          IndustryInformationClassification classification
    ) {
        boolean update = classificationService.updateById(classification);
        if (!update) {
            return R.error().message("更新失败");
        }
        return R.ok().data("success", true);
    }

    /**
     * 查询所有分类
     *
     * @return
     */
    @GetMapping("/getClassification")
    public R getClassification() {
        List<IndustryInformationClassification> list =
                classificationService.list(null);
        return R.ok().data("list", list);
    }

    /**
     * 将excel中的数据导入到数据库
     *
     * @param file
     * @return
     */
    @PostMapping("/getExcelData")
    public R getExcelData(MultipartFile file) {
        informationService.importClassificationData(file, classificationService, informationService);
        return R.ok();
    }

}

